<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="100px">
      <el-form-item label="档案编码" prop="recordCode">
        <el-input
v-model="queryParams.recordCode" placeholder="请输入档案编码" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
v-model="queryParams.companyName" placeholder="请输入公司名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="所属经济城" prop="economicZone">
        <el-input
v-model="queryParams.economicZone" placeholder="请输入所属经济城" clearable @blur="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="税务所" prop="taxOffice">
        <el-input
v-model="queryParams.taxOffice" placeholder="请输入税务所" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>

      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input
v-model="queryParams.legalPersonName" placeholder="请输入法人姓名" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="企业联系人" prop="companyContact">
        <el-input
v-model="queryParams.companyContact" placeholder="请输入企业联系人" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['oa:accounting-companies:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['oa:accounting-companies:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-upload
          :show-file-list="false"
          :before-upload="beforeImport"
          accept=".xlsx,.xls"
          v-hasPermi="['oa:accounting-companies:import']"
        >
          <el-button type="warning" plain>
            <Icon icon="ep:upload" class="mr-5px" /> 导入
          </el-button>
        </el-upload>

      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" width="100%">
<!--      <el-table-column label="序号" align="center" prop="id" width="60" />-->

      <el-table-column label="档案编号" align="center" prop="recordCode" min-width="200" />
      <el-table-column label="企业名称" align="center" prop="companyName" min-width="350" />
      <el-table-column label="统一社会信用代码" align="center" prop="unifiedSocialCreditCode" min-width="200" />
      <el-table-column label="所属经济城" align="center" prop="economicZone" min-width="150" />
      <el-table-column label="法人姓名" align="center" prop="legalPersonName" min-width="100" />
      <el-table-column label="委托开始时间" align="center" prop="entrustStartDate" min-width="120" />
      <el-table-column label="招商负责人" align="center" prop="investmentManager" min-width="100" />

      <!-- <el-table-column label="企业联系电话" align="center" prop="contactPhone" min-width="120" />
      <el-table-column label="公司类型" align="center" prop="companyType" min-width="280" />
      <el-table-column label="成立日期" align="center" prop="establishmentDate" min-width="120" />
      
      <el-table-column label="法人身份证号" align="center" prop="legalPersonId" min-width="180" />
      <el-table-column label="法人电话号码" align="center" prop="legalPersonPhone" min-width="120" /> -->
      
<!--      <el-table-column label="所属经济城" align="center" prop="economicZone" width="120" />-->
<!--      <el-table-column label="税务所" align="center" prop="taxOffice" width="120" />-->
<!--      <el-table-column label="注册资金" align="center" prop="registeredCapital" width="100">-->
<!--        <template #default="{ row }">-->
<!--          <span>{{ row.registeredCapital ? row.registeredCapital : '&#45;&#45;' }} 万</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="经营期限" align="center" prop="operationPeriod" width="120" />-->
<!--      <el-table-column label="开户账号" align="center" prop="bankAccount" width="180" />-->
<!--      <el-table-column label="法人姓名" align="center" prop="legalPersonName" width="100" />-->
<!--      <el-table-column label="法人电话号码" align="center" prop="legalPersonPhone" width="120" />-->
<!--      <el-table-column label="企业联系人" align="center" prop="companyContact" width="100" />-->
<!--      <el-table-column label="企业联系电话" align="center" prop="contactPhone" width="120" />-->
<!--      <el-table-column label="招商负责人" align="center" prop="investmentManager" width="100" />-->
<!--     -->
<!--      <el-table-column label="纳税人属性" align="center" prop="taxpayerType" width="100">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :type="DICT_TYPE.OA_TAXPAYER_ATTRIBUTE" :value="scope.row.taxpayerType" />-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="专管员" align="center" prop="taxSupervisor" width="100" />-->
<!--      <el-table-column label="注册地址" align="center" prop="registeredAddress" width="200" />-->
<!--      <el-table-column label="企业邮箱" align="center" prop="companyEmail" width="150" />-->
<!--      <el-table-column label="介绍人" align="center" prop="referrer" width="100" />-->
<!--      <el-table-column label="记账负责人" align="center" prop="accountingManager" width="100" />-->
<!--      <el-table-column label="统一社会信用代码" align="center" prop="unifiedSocialCreditCode" width="180" />-->
<!--      <el-table-column label="登记机关" align="center" prop="registrationAuthority" width="120" />-->
<!--      <el-table-column label="公司类型" align="center" prop="companyType" width="100" />-->
<!--      <el-table-column label="成立日期" align="center" prop="establishmentDate" width="120" />-->
<!--      <el-table-column label="开户银行" align="center" prop="bankName" width="150" />-->
<!--      <el-table-column label="法人身份证号" align="center" prop="legalPersonId" width="180" />-->
<!--      <el-table-column label="收费价格(月)" align="center" prop="monthlyFee" width="100">-->
<!--        <template #default="{ row }">-->
<!--          <span>{{ row.monthlyFee ? row.monthlyFee : '&#45;&#45;' }} 元</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <!-- <el-table-column label="网厅财务负责人" align="center" prop="onlineFinanceManager" width="120" /> -->
<!--      <el-table-column label="创建者" align="center" prop="creatorName" width="100" />-->
<!--      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="160" />-->
<!--      <el-table-column label="更新者" align="center" prop="updaterName" width="100" />-->
      <el-table-column label="操作" align="center" min-width="180" fixed="right">
        <template #default="scope">
          <el-button
link type="primary" @click="openDetail(scope.row.id)"
            v-hasPermi="['oa:accounting-companies:query']">
            查看
          </el-button>
          <el-button
link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['oa:accounting-companies:update']">
            编辑
          </el-button>
          <el-button
link type="danger" @click="handleDelete(scope.row.id)"
            v-hasPermi="['oa:accounting-companies:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <AccountingCompaniesForm ref="formRef" @success="getList" />
  <!-- 详情弹窗 -->
  <AccountingCompaniesDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { AccountingCompaniesApi, AccountingCompaniesVO } from '@/api/oa/accountingcompanies'
import AccountingCompaniesForm from './AccountingCompaniesForm.vue'
import AccountingCompaniesDetail from './AccountingCompaniesDetail.vue'

/** 记账企业信息 列表 */
defineOptions({ name: 'AccountingCompanies' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<AccountingCompaniesVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  recordCode: undefined,
  economicZone: undefined,
  taxOffice: undefined,
  legalPersonName: undefined,
  companyContact: undefined,
  companyName: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AccountingCompaniesApi.getAccountingCompaniesPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 查看详情操作 */
const detailRef = ref()
const openDetail = (id: number) => {
  detailRef.value.open(id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AccountingCompaniesApi.deleteAccountingCompanies(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await AccountingCompaniesApi.exportAccountingCompanies(queryParams)
    download.excel(data, '记账企业信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 导入按钮操作 */
const beforeImport = async (file: File) => {
  try {
    const formData = new FormData()
    formData.append('file', file)
    await AccountingCompaniesApi.importAccountingCompanies(formData)
    message.success('导入成功')
    await getList()
  } catch (error: any) {
    message.error('导入失败：' + (error.message || '未知错误'))
  }
  return false // 阻止默认上传行为
}

/** 下载模板按钮操作 */
const downloadTemplate = async () => {
  try {
    const data = await AccountingCompaniesApi.downloadImportTemplate()
    download.excel(data, '记账企业信息_导入模板.xls')
  } catch (error: any) {
    message.error('下载模板失败：' + (error.message || '未知错误'))
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
